﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using studentSubsidy.Entity.Apply;
using studentSubsidy.Models;
using studentSubsidy.Utils;

namespace studentSubsidy.Dao
{
    public class ApplyDao
    {
        private MyDbContext DbContext = new MyDbContext();

        public List<Apply> isHasApply(int projectId, int user_id)
        {
            List<Apply> isHas = DbContext.Applies.Where(p =>
                p.project_id == projectId && p.user_id == user_id).ToList();
            return isHas;
        }

        public void insertApply(ApplyQuery applyQuery)
        {
            int isCityCheck = 0;
            Project project = DbContext.Project.Where(p => p.product_id == applyQuery.productId).ToList().First();
            if (project.category_id == 1 || project.category_id == 3)
            {
                isCityCheck = -1;
            }

            var apply = new Apply
            {
                apply_id = GenerateID.GenForInt(),
                project_id = applyQuery.productId,
                user_id = applyQuery.userId,
                bank_number = applyQuery.bankNumber,
                check_progress = 1,
                isCityCheck = isCityCheck,
                isCheck = 0,
                isSigned = 0,
                isPayment = 0,
                fail_remark = null,
                apply_time = DateTime.Now.ToString("s")
            };

            DbContext.Applies.Add(apply);
            DbContext.SaveChanges();
        }

        public List<Apply> getApplyProject(int user_id)
        {
            var projectIdList = DbContext.Applies.Where(p => p.user_id == user_id && p.isCheck == 0).Select(p =>
                new Apply
                {
                    project_id = p.project_id,
                    check_progress = p.check_progress
                }).ToList();
            return projectIdList;
        }

        public Project getProjectById(int project_id)
        {
            var project = DbContext.Project.Where(p => p.product_id == project_id).ToList();
            return project.First();
        }

        public User getUser(int user_id)
        {
            var users = DbContext.User.Where(p => p.id == user_id).ToList();
            return users.First();
        }

        public Apply getApplyById(int userId, int projectId)
        {
            var applies = DbContext.Applies.Where(p => p.project_id == projectId && p.user_id == userId).ToList();
            return applies.First();
        }

        public Apply getApplyByApplyId(int applyId)
        {
            var applies = DbContext.Applies.Where(p => p.apply_id == applyId).ToList();
            return applies.First();
        }

        public List<Apply_File> getApplyFile(ApplyDetailQuery applyDetailQuery)
        {
            var applyfiles = DbContext.UploadFiles.Where(p =>
                p.project_id == applyDetailQuery.projectId && p.user_id == applyDetailQuery.userId).ToList();
            return applyfiles;
        }

        public void deleteApplyFile(ApplyDetailQuery applyDetailQuery)
        {
            DbContext.UploadFiles.Where(p =>
                p.project_id == applyDetailQuery.projectId && p.user_id == applyDetailQuery.userId).ExecuteDelete();
        }

        public void deleteApply(ApplyDetailQuery applyDetailQuery)
        {
            DbContext.Applies.Where(p =>
                p.project_id == applyDetailQuery.projectId && p.user_id == applyDetailQuery.userId).ExecuteDelete();
        }

        public List<Apply> getApplyWithoutCheck(string role)
        {
            if (role == "school")
            {
                return DbContext.Applies.Where(p => p.isCheck == 0 && p.check_progress == 1).ToList();
            }

            if (role =="bureau")
            {
                return DbContext.Applies.Where(p => p.isCheck == 0 && p.check_progress == 2).ToList();
            }

            return null;
        }

        public void updateCheckProgress(int apply_id, int progress_check,int isCheck,int isCityCheck)
        {
            Apply apply = DbContext.Applies.Where(p => p.apply_id == apply_id).ToList().First();
            apply.check_progress = progress_check;
            apply.isCheck = isCheck;
            apply.isCityCheck = isCityCheck;
            EntityState ss1 = DbContext.Entry(apply).State;
            DbContext.SaveChanges();
        }

        public List<Apply> hasCheckApply(int user_id)
        {
            var projectIdList = DbContext.Applies.Where(p => p.user_id == user_id && p.isCheck == 1).Select(p =>
                new Apply
                {
                    project_id = p.project_id,
                    check_progress = p.check_progress
                }).ToList();
            return projectIdList;
        }

        public List<Apply> getRejectList(int user_id)
        {
            var projectIdList = DbContext.Applies.Where(p => p.user_id == user_id && p.isCheck == -1).Select(p =>
                new Apply
                {
                    project_id = p.project_id,
                    check_progress = p.check_progress
                }).ToList();
            return projectIdList;
        }

        public List<Apply> getNeedSignedList(int userId)
        {
            return DbContext.Applies.Where(p => p.user_id == userId && p.isSigned == 2).ToList();
        }
        
        public List<Apply> getHasSignedCheckList(int userId)
        {
            return DbContext.Applies.Where(p => p.user_id == userId && p.isSigned == 1).ToList();
        }
    }
}